package internal

import (
	"fmt"
	"reflect"
	"server/msg"
)

func init() {
	// 玩家交流
	handleMsg(&msg.CG_game_ChatSend{}, game_ChatSendHandler)

	// 心跳请求
	handleMsg(&msg.CG_game_Heartbeat{}, game_heartbeatHandler)
}

func handleMsg(m interface{}, h interface{}) {
	skeleton.RegisterChanRPC(reflect.TypeOf(m), h)
}

// 聊天内容
func game_ChatSendHandler(args []interface{}) {
	// PlayerManagerObj.Players.BoardMessage
	agent := args[0].(*msg.CG_game_ChatSend)
	fmt.Println(agent, agent.Context, agent.Msg_type, agent.Name)
	if agent.Msg_type == 1 {
		//退出
		key := agent.Name
		if _, ok := PlayerManagerObj.Players[key]; ok {
			(*PlayerManagerObj.Players[key].Agent).Close()
			delete(PlayerManagerObj.Players, key)
			fmt.Println("Player deleted successfully")
		} else {
			fmt.Println("Player not found")
		}
		PlayerManagerObj.BoardMessage(agent.Msg_type, agent.Name, " leave room!")
	} else if agent.Msg_type == 2 {
		//聊天
		PlayerManagerObj.BoardMessage(agent.Msg_type, agent.Name, agent.Context)
	}
}

// 心跳请求
func game_heartbeatHandler(args []interface{}) {
	req := args[0].(*msg.CG_game_Heartbeat)
	PlayerManagerObj.Heartbeat(req)
}
